/**
 * @file hopper_ev2000_dll.h
 * @mainpage hopper_ev2000_boost_dll
 * @par Description:
 * dll to work with hopper EV2000 via cctalk interface
 *
 * using sequence example:
 * - hopper_init(void)
 * - hopper_d2xx_open(char *vpPort_name)
 * - hopper_prepare(char *vp_Port_name)
 * - hopper_put_coins_ack(char addr, int Coins_count, int *Coins_rem, int *Coins_paid, int *Coins_unpaid)
 * - hopper_close(void)
 *
 * or
 * - hopper_init_prepare_mode_d2xx(int mode, int inp_par, int port_No, char * vpPort_name);
 * - hopper_init_prepare_mode_d2xx_portNo(int mode, int port_No);
 * - hopper_put_coins_ack(char addr, int Coins_count, int *Coins_rem, int *Coins_paid, int *Coins_unpaid)
 * - hopper_close(void)
 *
 **/
/*
 * hopper_ev2000_dll.h
 *
 *  Created on: 24.12.2012
 *      Author: aska
 */

#ifndef HOPPER_EV2000_DLL_H_
#define HOPPER_EV2000_DLL_H_
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <string>

using namespace std;
/*
#ifdef HOPPER_EV2000_DLL_H_EXPORTS
#define HOPPER_EV2000_DLL_H_API __declspec(dllexport)
#else
#define HOPPER_EV2000_DLL_H_API __declspec(dllimport)
#endif
*/
#ifdef __cplusplus
extern "C"
{
#endif
//  Errors returned by functions that accesses to cctalk
//  0 - no errors
//  1 - checksum error
//  2 - no data received
//  3 - other exception
//  254 - port is not opened
//  255 - hopper is not initialized
__declspec(dllexport) BOOL WINAPI __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
//__declspec(dllexport) int __stdcall door_control_dev_if_mode(int mode);

/**\brief Init
 *
 * Initialization library
 * Calls Hopper constructor
 *
 * @return
 * - 1 if object is created before
 * - 0 if object is created
 */
//HOPPER_EV2000_DLL_H_API
__declspec(dllexport) int __stdcall hopper_init(void);
/**\brief Initialization, set mode d2xx and prepare
 *
 * Initializes library: calls Hopper constructor<br>
 * sets mode to d2xx<br>
 * prepares hoppers with address 3 and 4
 *
 * @param int mode: 1 - VCP, 2 - D2XX
 * @param int inp_par - type of input parameter: 1 - use serial No, 2 - use serial name
 * @param int port_No - serial No
 * @param char * vp_Port_name - FTDI device port name in system
 * @return
 *  - 0 - OK
 *  else set bits:
 *   - 0 - init failed
 *   - 1 - set_mode failed
 *   - 8 : hopper 3 Simple_poll failed
 *   - 9 : hopper 4 Simple_poll failed
 *   - 10 : port open failed
 *   - 11...15 : errors from hopper_port_open:
 *    - 1 - unable to open port
 *    - 2 - unable to get port state
 *    - 3 - unable to get timeouts
 *    - 4 - unable to create timeouts
 *    - 5 - unable to set port paramters
 *    - 6 - unable to set timeouts
 *    - 7 - unable to purge buffer
 *    example for bits 15...8:
 *    unable to open port
 *      b00010100 (d20)
 *       |||| |--- 2nd bit is set - errors while opening port
 *       \__/ value 1 means "unable to open port"
 *
 */
//HOPPER_EV2000_DLL_H_API
__declspec(dllexport) int __stdcall hopper_init_prepare_mode_d2xx(int mode, int inp_par, int port_No, char * vpPort_name);
/**\brief Initialization, set mode d2xx and prepare
 * calls hopper_init_prepare_mode_d2xx(int mode, int inp_par, int port_No, char * vpPort_name)
 * with parameters inp_par = 1 and vpPort_name = ""
 */
__declspec(dllexport) int __stdcall hopper_init_prepare_mode_d2xx_portNo(int mode, int port_No);
/**\brief Close
 *
 * finalization library
 *
 * @return
 * - 0 - OK
 * - 1 - object not created
 */
//HOPPER_EV2000_DLL_H_API
__declspec(dllexport) int __stdcall hopper_close(void);
/**\brief Set mode
 *
 * Set FTDI connection mode
 *
 * @param[in] char dev_interface_mode_inp - 1 - VCP, 2 - D2XX
 * @return
 * - 0 - OK
 * - 255 - library not initialized
 */
__declspec(dllexport) int __stdcall hopper_set_mode(char dev_interface_mode_inp);
/**\brief Open serial port
 *
 * Opens serial port
 *
 * @param[in] char * vpPort_name - port name
 * @return
 * - 0 - OK
 */
//HOPPER_EV2000_DLL_H_API
__declspec(dllexport) int __stdcall hopper_port_open(char * vpPort_name);
/**\brief Open d2xx device
 *
 * Opens FTDI through d2xx library. Extended functionality achieved.
 *
 * //@param[in] char * vpPort_name - serial port name in system used to identify device
 * @return
 * - 0 - OK
 */
__declspec(dllexport) int __stdcall hopper_d2xx_open();
/**\brief request simple poll
 *
 *
 * @param[in] int addr - address of requested hopper
 * @return
 * - 0 - OK
 * - 255 if hopper not created
 */
__declspec(dllexport) int __stdcall hopper_send_simple_poll(char addr);
/**\brief address poll
 *
 *
 * @param[in] char * addr - address of requested hopper
 * @return
 * - 0 - OK
 * - 255 if hopper not created
 */
__declspec(dllexport) int __stdcall hopper_address_poll(char * addr);
/**\brief prepare hoppers with addresses 3 and 4
 *
 * @param int inp_par - type of input parameter: 1 - use serial No, 2 - use serial name
 * @param int port_No - serial No
 * @param char * vp_Port_name - FTDI device port name in system
 * @return
 *  - 0 - OK
 *  else set bits:
 *   - 0 : hopper 3 Simple_poll failed
 *   - 1 : hopper 4 Simple_poll failed
 *   - 2 : port open failed
 *   - 3...7 : errors from hopper_port_open:
 *    - 1 - unable to open port
 *    - 2 - unable to get port state
 *    - 3 - unable to get timeouts
 *    - 4 - unable to create timeouts
 *    - 5 - unable to set port paramters
 *    - 6 - unable to set timeouts
 *    - 7 - unable to purge buffer
 *    example:
 *    unable to open port
 *      b00010100 (d20)
 *       |||| |--- 2nd bit is set - errors while opening port
 *       \__/ value 1 means "unable to open port"
 */
// пример:
//	не удалось открыть порт
//	b00010100 (d20)
//     |||| |--- 2-ой бит установлен - ошибки при открытии порта
//     \__/ значение 1 - не удалось открыть порт
__declspec(dllexport) int __stdcall hopper_prepare(int inp_par, int port_No, char * vp_Port_name);
__declspec(dllexport) int __stdcall hopper_prepare_addr(char addr);
__declspec(dllexport) int __stdcall hopper_request_comms_revision(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_comm_status_variables(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_clear_comms_variables(char addr);
__declspec(dllexport) int __stdcall hopper_reset_device(char addr);
__declspec(dllexport) int __stdcall hopper_request_manufacturer_id(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_equipment_category_id(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_product_code(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_serial_nr(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_software_revision(char addr, char vp_byte_arr[]);
//...
/*\brief get hopper test
 * char vp_hopper_registers[]:
 * - [0] - hopper address
 * - [1] - status register 1
 * - [2] - status register 2
 *
 */
__declspec(dllexport) int __stdcall hopper_test_hopper(char addr, char vp_hopper_registers[]);
/*\brief get hopper status
 * char vp_byte_arr[]:
 * - [0] - hopper address
 * - [1] - event counter
 * - [2] - payout coins remaining
 * - [3] - last payout: coins paid
 * - [4] - last payout: coins unpaid
 */
__declspec(dllexport) int __stdcall hopper_request_status(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_emergency_stop(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_empty_hopper(char addr, int coins_to_put);
__declspec(dllexport) int __stdcall hopper_request_payout_hi_lo_status(char addr, char vp_byte_arr[]);
__declspec(dllexport) int __stdcall hopper_request_payout_hi_status(char addr, bool * out_bool);
__declspec(dllexport) int __stdcall hopper_request_payout_lo_status(char addr, bool * out_bool);
//...
__declspec(dllexport) int __stdcall hopper_request_dispense_count(char addr, char vp_byte_arr[]);
//...
/**\brief put coins with acknowledge
 *
 * Put coins, wait for acknowledge and return results
 * @param[in] char addr - address of requested hopper
 * @param[in] int Coins_count - coins to pay
 * @param[out] int * Coins_rem - coins remaining to pay
 * @param[out] int * Coins_paid - coins paid
 * @param[out] int * Coins_unpaid - coins unpaid from coins was requested in last group
 */
__declspec(dllexport) int __stdcall hopper_put_coins_ack(char addr, int Coins_count,
		int * Coins_rem, int * Coins_paid, int * Coins_unpaid);
__declspec(dllexport) bool __stdcall hopper_get_high_sensor(char addr);
__declspec(dllexport) bool __stdcall hopper_get_low_sensor(char addr);
__declspec(dllexport) int __stdcall hopper_get_error(byte addr);

#ifdef __cplusplus
}
#endif

#endif /* HOPPER_EV2000_DLL_H_ */
